package com.imooc.miaosha.dao;

import com.imooc.miaosha.domain.MiaoshaGoods;
import com.imooc.miaosha.vo.GoodsVo;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author liyuhui
 */
@Mapper
public interface GoodsDao {

    /**
     * 查询出商品信息和秒杀信息
     * @return
     */
    @Select(" select g.*," +
            " mg.miaosha_stock,mg.start_date, mg.end_date,mg.miaosha_price from miaosha_goods mg " +
            " left join goods g " +
            " on mg.goods_id = g.id ")
    List<GoodsVo> listGoodsVo();


    /**
     * 查询出指定商品信息和秒杀信息
     * @return
     */
    @Select(" select g.*, " +
            " mg.miaosha_stock,mg.start_date, mg.end_date,mg.miaosha_price from miaosha_goods mg " +
            " left join goods g " +
            " on mg.goods_id = g.id " +
            " where g.id = #{goodsId} ")
    GoodsVo getGoodsVoByGoodsId(@Param("goodsId")long goodsId);

    /**
     * 减少秒杀商品的库存量
     * @param g
     * @return
     */
    @Update(" update miaosha_goods " +
            " set miaosha_stock = miaosha_stock - 1 " +
            " where goods_id = #{goodsId} and miaosha_stock > 0 ")
    int reduceStock(MiaoshaGoods g);

    /**
     * 更新秒杀商品的库存
     * @param g
     * @return
     */
    @Update(" update miaosha_goods " +
            " set miaosha_stock = #{miaoshaStock} " +
            " where goods_id = #{goodsId} ")
    int resetStock(MiaoshaGoods g);
}
